package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.query.BpmnInfoQueryObject;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 控制器
 *
 * @author RNIZ
 */
@Controller
@RequestMapping("business/bpmnInfo")
public class BpmnInfoController {
    /**
     * 模板前缀
     */
    private static final String PREFIX = "/business/bpmnInfo/";

    @Autowired
    private IBpmnInfoService bpmnInfoService;

    /**
     * 页面
     * 列表
     */
    @RequiresPermissions("business:bpmnInfo:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return PREFIX + "list";
    }

    @RequiresPermissions("business:bpmnInfo:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        model.addAttribute("bpmnInfo", bpmnInfoService.get(id));
        return PREFIX + "edit";
    }

    /**
     * 数据
     * 列表
     */
    @RequiresPermissions("business:bpmnInfo:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BpmnInfo> query(BpmnInfoQueryObject qo) {
        return bpmnInfoService.query(qo);
    }


    /**
     * 编辑
     *
     * @param bpmnInfo 服务项目
     * @return {@link AjaxResult}
     */
    @RequiresPermissions("business:bpmnInfo:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(BpmnInfo bpmnInfo) {
        bpmnInfoService.update(bpmnInfo);
        return AjaxResult.success();
    }

    /**
     * 删除
     *
     * @param id 语句id
     * @return {@link AjaxResult}
     */
    @RequiresPermissions("business:bpmnInfo:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult remove(Long id) {
        bpmnInfoService.deleteBatch(id);
        return AjaxResult.success();
    }

    /**
     * 部署页面
     *
     * @param id id
     */
    @RequiresPermissions("business:bpmnInfo:deployPage")
    @RequestMapping("/deployPage")
    public String deployPage(Long id,Model model) {
        model.addAttribute("bpmnInfo",bpmnInfoService.get(id));
        return PREFIX + "deploy";
    }
    @RequiresPermissions("business:bpmnInfo:upload")
    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) throws IOException {
        //判断文件是否为空
        if(file != null && file.getSize() > 0){
            //获取文件名字
            String originalFilename = file.getOriginalFilename();
            //获取文件的后缀
            String extName = FilenameUtils.getExtension(originalFilename);
//            String extName = FileUploadUtils.getExtension(file);
            //判断上传文件类型是否正确
            if("bpmn".equalsIgnoreCase(extName) ){
                String uploadPath = FileUploadUtils.upload(SystemConfig.getUploadPath(), file);
                return AjaxResult.success("上传成功",uploadPath);
            }else{
                return AjaxResult.error("流程定义文件仅支持 bpmn格式！");
            }

        }else{
            return AjaxResult.error("不允许上传空文件！");
        }
    }

    /**
     * 部署
     *
     * @param path     路径
     * @param bpmnType bpmn类型
     * @param info     信息
     * @return {@link AjaxResult}
     * @throws FileNotFoundException 文件未发现异常
     */
    @RequiresPermissions("business:bpmnInfo:deploy")
    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(String path,String bpmnType,String info) throws FileNotFoundException {
        bpmnInfoService.insert(path,bpmnType,info);
        return AjaxResult.success();
    }

    /**
     * 阅读资源
     *
     * @param deploymentId 部署id
     * @param type         类型
     * @param response     响应
     * @throws IOException ioexception
     */
    @RequiresPermissions("business:bpmnInfo:readResource")
    @RequestMapping("/readResource")
    @ResponseBody
    public void readResource(String deploymentId, String type, HttpServletResponse response) throws IOException {
        InputStream inputStream = bpmnInfoService.getResourceAsStream(deploymentId,type);
        IOUtils.copy(inputStream,response.getOutputStream());
    }

}
